/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Austin
 */
public class TimeFormat {

    public long getTimestamp(String sTime) {
        long rand = -99;
        try {
            Timestamp ts = Timestamp.valueOf(sTime);
            Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
            long l = ts.getTime() - date2.getTime() > 0 ? ts.getTime() - date2.getTime() : date2.getTime() - ts.getTime();
            rand = (int) (l / 1000);

        } catch (ParseException ex) {
            Logger.getLogger(TimeFormat.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return rand;
        }
    }

    //获取当前系统时间
    public long getNewTime() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return new Date(sdf.format(d)).getTime();
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(new TimeFormat().getTimestamp("2017-04-11 :00:00"));
        System.out.println(new TimeFormat().getNewTime());
        System.out.println(new Date("2017/03/31 15:13:00").getTime());
    }
}
